package top.dprkshow.practice.graph;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Kent HAN
 * @date 2021/9/9
 */
public class MatrixGraph {
    private final int totalVertices;
    private int[][] matrix;

    public MatrixGraph(int totalVertices) {
        this.totalVertices = totalVertices;
        matrix = new int[totalVertices][totalVertices];
    }

    public void addEdge(int vertexA, int vertexB) {
        matrix[vertexA][vertexB] = 1;
        matrix[vertexB][vertexA] = 1;
    }

    public List<Integer> adj(int vertex) {
        ArrayList<Integer> adjList = new ArrayList<>();
        for (int i = 0; i < matrix[vertex].length; i++) {
            if (matrix[vertex][i] == 1) {
                adjList.add(i);
            }
        }
        return adjList;
    }

    public int getTotalVertices() {
        return matrix.length;
    }
}
